﻿using ExcelSystem.Model;
using ExcelSystem.ToolClass;
using ExcelSystem.ToolHelper;
using ExcelSystem.ViewModelsClass;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ExcelSystem.ViewModels
{
    public class BOMInfoViewModel : BindableBase, IDialogAware
    {

        ExcelContext db;
        public string Title => "参数修改";

        public event Action<IDialogResult> RequestClose;


        private List<BOM_Info> bomPar;
        public List<BOM_Info> BOMPar
        {
            get { return bomPar; }
            set { SetProperty(ref bomPar, value); }
        }

        private List<Formula> baseFormula;
        public List<Formula> BaseFormula
        {
            get { return baseFormula; }
            set { SetProperty(ref baseFormula, value); }
        }

        private List<Formula> commonFormula;
        public List<Formula> CommonFormula
        {
            get { return commonFormula; }
            set { SetProperty(ref commonFormula, value); }
        }
        private List<FormulaType> formulaTypes;
        public List<FormulaType> FormulaTypes
        {
            get { return formulaTypes; }
            set { SetProperty(ref formulaTypes, value); }
        }

        private BOM bom;
        private DelegateCommand saveCommand;
        /// <summary>
        /// 保存修改
        /// </summary>
        public DelegateCommand SaveCommand
        {
            get { return saveCommand; }
            set { SetProperty(ref saveCommand, value); }
        }

       
        BOMInfoViewModel()
        {

            //数据库文档
            db = ExcelContentFactory.GetInstance();
            BOMPar = new List<BOM_Info>();
            FormulaTypes = new List<FormulaType> { new FormulaType() { TypeID=0, Name="数字计算" },
            new FormulaType() { TypeID=2, Name="尺寸" },
            new FormulaType() { TypeID=5, Name="枚举" }
            };
            //基础公式
            BaseFormula = db.Formulas.Where(t => t.IsBase == true).ToList();
            //通用公式
            CommonFormula= db.Formulas.Where(t => t.IsBase == false).ToList();
            SaveCommand = new DelegateCommand(Save);

         

        }

        private void Save()
        {
          bom.ChildPar=JsonConvert.SerializeObject(BOMPar);
            //db.Formulas.AddRange(CommonFormula.Where(t => t.ID == 0).ToList());
            db.Formulas.UpdateRange(CommonFormula);
            db.SaveChanges();
            MessageBox.Show("保存成功");
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            
            if (parameters != null&& parameters.ContainsKey("id")) {

               bom=  db.BOMs.FirstOrDefault(t => t.MaterialNo == parameters.GetValue<string>("id"));
                if(bom.Company==2&& bom.ChildPar!=null)
                {
                    BOMPar = JsonConvert.DeserializeObject<List<BOM_Info>>(bom.ChildPar);
                }
                else if(bom.Par!=null && bom.ChildPar != null)
                {
                    BOMPar = JsonConvert.DeserializeObject<List<BOM_Info>>(bom.ChildPar) ;
                }
            }
        }
        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }
    }
}
